<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-big-counter.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"raphets.github.io","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"hide","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="最近使用python爬虫，使用的是mongodb，经常需要通过python代码操作mongodb，记录一下，方便以后查找。">
<meta property="og:type" content="article">
<meta property="og:title" content="Python 操作 Mongodb 详解">
<meta property="og:url" content="https://raphets.github.io/archives/49500.html">
<meta property="og:site_name" content="尚拙">
<meta property="og:description" content="最近使用python爬虫，使用的是mongodb，经常需要通过python代码操作mongodb，记录一下，方便以后查找。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-04-15T09:28:45.000Z">
<meta property="article:modified_time" content="2022-05-25T02:42:58.025Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="mongodb">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://raphets.github.io/archives/49500.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Python 操作 Mongodb 详解 | 尚拙</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">尚拙</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">一个分享技术、学习成长的个人博客网站</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-tool">

    <a href="/tool/" rel="section"><i class="fa fa-hammer fa-fw"></i>工具</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://raphets.github.io/archives/49500.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="John Doe">
      <meta itemprop="description" content="不积跬步，无以至千里；不积小流，无以成江海。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="尚拙">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Python 操作 Mongodb 详解
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-04-15 17:28:45" itemprop="dateCreated datePublished" datetime="2019-04-15T17:28:45+08:00">2019-04-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-05-25 10:42:58" itemprop="dateModified" datetime="2022-05-25T10:42:58+08:00">2022-05-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/" itemprop="url" rel="index"><span itemprop="name">技术文章</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/Python/" itemprop="url" rel="index"><span itemprop="name">Python</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>最近使用python爬虫，使用的是mongodb，经常需要通过python代码操作mongodb，记录一下，方便以后查找。</p>
<span id="more"></span>

<h1 id="一、准备工作"><a href="#一、准备工作" class="headerlink" title="一、准备工作"></a>一、准备工作</h1><p>1、安装mongodb</p>
<p>下载地址：<a target="_blank" rel="noopener" href="https://www.mongodb.com/download-center/community">https://www.mongodb.com/download-center/community</a></p>
<p>安装过程就不写了，可以参考这个<a target="_blank" rel="noopener" href="http://www.runoob.com/mongodb/mongodb-window-install.html">http://www.runoob.com/mongodb/mongodb-window-install.html</a></p>
<p>2、安装python中MongoDB的驱动程序</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">pip install pymongo</span><br></pre></td></tr></table></figure>

<p>3、启动mongodb服务</p>
<h1 id="二、连接Mongodb"><a href="#二、连接Mongodb" class="headerlink" title="二、连接Mongodb"></a>二、连接Mongodb</h1><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">import pymongo</span><br><span class="line"></span><br><span class="line">conn = pymongo.MongoClient(&#x27;localhost&#x27;, 27017)</span><br><span class="line"></span><br><span class="line">my_db = conn[&quot;test_db&quot;]</span><br><span class="line"></span><br><span class="line">my_collection = my_db[&quot;test_collection&quot;]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="1、连接MongoDB"><a href="#1、连接MongoDB" class="headerlink" title="1、连接MongoDB"></a>1、连接MongoDB</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">conn = pymongo.MongoClient(&#x27;localhost&#x27;, 27017)</span><br></pre></td></tr></table></figure>

<h4 id="2、新建一个数据库，或者使用某个数据库"><a href="#2、新建一个数据库，或者使用某个数据库" class="headerlink" title="2、新建一个数据库，或者使用某个数据库"></a>2、新建一个数据库，或者使用某个数据库</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">my_db = conn[&quot;test_db&quot;]</span><br></pre></td></tr></table></figure>

<h4 id="3、新建一个集合，或者使用数据库中的某个集合"><a href="#3、新建一个集合，或者使用数据库中的某个集合" class="headerlink" title="3、新建一个集合，或者使用数据库中的某个集合"></a>3、新建一个集合，或者使用数据库中的某个集合</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">my_collection = my_db[&quot;test_collection&quot;]</span><br></pre></td></tr></table></figure>

<h1 id="三、添加数据"><a href="#三、添加数据" class="headerlink" title="三、添加数据"></a>三、添加数据</h1><h4 id="插入一条数据"><a href="#插入一条数据" class="headerlink" title="插入一条数据"></a>插入一条数据</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">data1 = &#123;</span><br><span class="line">          &quot;name&quot;: “zhangsan”,</span><br><span class="line">          &quot;age&quot;: 10,</span><br><span class="line">         &#125;</span><br><span class="line">        </span><br><span class="line">my_collection.insert_one(data)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>注：插入的数据必须是字典格式  </p>
<h4 id="插入多条数据"><a href="#插入多条数据" class="headerlink" title="插入多条数据"></a>插入多条数据</h4>  <figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">data1 = &#123;</span><br><span class="line">          &quot;name&quot;: “zhangsan”,</span><br><span class="line">          &quot;age&quot;: 10,</span><br><span class="line">         &#125;</span><br><span class="line">          </span><br><span class="line">data2 = &#123;</span><br><span class="line">          &quot;name&quot;:  “lisi”,</span><br><span class="line">          &quot;age&quot;: 24,</span><br><span class="line">         &#125;      </span><br><span class="line">          </span><br><span class="line">my_collection.insert_many([data1,date2])</span><br><span class="line">    </span><br></pre></td></tr></table></figure>
<h1 id="四、查询数据"><a href="#四、查询数据" class="headerlink" title="四、查询数据"></a>四、查询数据</h1><h4 id="查询所有数据"><a href="#查询所有数据" class="headerlink" title="查询所有数据"></a>查询所有数据</h4><p> 例子：查询my_collection集合所有数据</p>
 <figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find()</span><br></pre></td></tr></table></figure>
<p> 注：查询结果为字典集合</p>
<h4 id="查询一条数据"><a href="#查询一条数据" class="headerlink" title="查询一条数据"></a>查询一条数据</h4><p>例子：查询my_collection集合中“name”为”zhangsan”的第一条数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find_one(&#123;&#x27;name&#x27;: ‘zhangsan’&#125;)</span><br></pre></td></tr></table></figure>
<p>注：查询结果为字典对象</p>
<h4 id="查询多条数据"><a href="#查询多条数据" class="headerlink" title="查询多条数据"></a>查询多条数据</h4><p>例子：查询my_collection集合中“age”为10的所有数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find(&#123;&#x27;age&#x27;:10&#125;)</span><br></pre></td></tr></table></figure>
<p>注： 查询结果为字典集合</p>
<h4 id="条件查询"><a href="#条件查询" class="headerlink" title="条件查询"></a>条件查询</h4><table>
<thead>
<tr>
<th align="center">符号</th>
<th align="center">含义</th>
<th align="center">示例</th>
</tr>
</thead>
<tbody><tr>
<td align="center">$lt</td>
<td align="center">小于</td>
<td align="center">{‘age’: {‘$lt’: 20}}</td>
</tr>
<tr>
<td align="center">$gt</td>
<td align="center">大于</td>
<td align="center">{‘age’: {‘$gt’: 20}}</td>
</tr>
<tr>
<td align="center">$lte</td>
<td align="center">小于等于</td>
<td align="center">{‘age’: {‘$lte’: 20}}</td>
</tr>
<tr>
<td align="center">$gte</td>
<td align="center">大于等于</td>
<td align="center">{‘age’: {‘$gte’: 20}}</td>
</tr>
<tr>
<td align="center">$ne</td>
<td align="center">不等于</td>
<td align="center">{‘age’: {‘$ne’: 20}}</td>
</tr>
<tr>
<td align="center">$in</td>
<td align="center">在范围内</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">$nin</td>
<td align="center">不在范围内</td>
<td align="center"></td>
</tr>
</tbody></table>
<p>例子：查询my_collection集合中“age”大于10的所有数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find(&#123;&#x27;age&#x27;: &#123;&#x27;$gt&#x27;: 10&#125;&#125;)</span><br></pre></td></tr></table></figure>
<p>注： 查询结果为字典集合</p>
<h4 id="或"><a href="#或" class="headerlink" title="或"></a>或</h4><p>例子：查询my_collection集合中“age”为20或者35的所有数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find(&#123;&quot;$or&quot;:[&#123;&quot;age&quot;:20&#125;,&#123;&quot;age&quot;:35&#125;]&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="与"><a href="#与" class="headerlink" title="与"></a>与</h4><p>例子：查询my_collection集合中“name”为”zhangsan”并且“age”为35的所有数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find(&#123;&quot;$and&quot;:[&#123;&quot;name&quot;:&quot;zhangsan&quot;&#125;,&#123;&quot;age&quot;:35&#125;]&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="使用正则表达式查询"><a href="#使用正则表达式查询" class="headerlink" title="使用正则表达式查询"></a>使用正则表达式查询</h4><p>例子：查询my_collection集合中“name”中第一个字母为“z”的所有数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">myquery = &#123; &quot;name&quot;: &#123; &quot;$regex&quot;: &quot;^z&quot; &#125; &#125;</span><br><span class="line">result = my_collection.find(myquery)</span><br></pre></td></tr></table></figure>
<p> 查询结果为字典集合</p>
<h4 id="返回指定条数记录"><a href="#返回指定条数记录" class="headerlink" title="返回指定条数记录"></a>返回指定条数记录</h4><p>如果我们要对查询结果设置指定条数的记录可以使用 limit() 方法，该方法只接受一个数字参数。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find().limit(3)</span><br></pre></td></tr></table></figure>

<h1 id="五、修改数据"><a href="#五、修改数据" class="headerlink" title="五、修改数据"></a>五、修改数据</h1><h4 id="修改一条数据"><a href="#修改一条数据" class="headerlink" title="修改一条数据"></a>修改一条数据</h4><p> 例子：将my_collection集合中“name”为“zhangsan”的第一条数据的年龄修改为50<br> <figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">myquery = &#123; &quot;name&quot;: &quot;zhangsan&quot; &#125;</span><br><span class="line">newvalues = &#123; &quot;$set&quot;: &#123; &quot;age&quot;: 50 &#125; &#125;</span><br><span class="line">my_collection.update_one(myquery, newvalues) </span><br></pre></td></tr></table></figure></p>
<h4 id="修改多条数据"><a href="#修改多条数据" class="headerlink" title="修改多条数据"></a>修改多条数据</h4><p> 例子：将my_collection集合中“name”为“lisi”的所有数据的年龄修改为50<br> <figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">myquery = &#123; &quot;name&quot;: &quot;lisi&quot; &#125;</span><br><span class="line">newvalues = &#123; &quot;$set&quot;: &#123; &quot;age&quot;: 50 &#125; &#125;</span><br><span class="line">my_collection.update_many(myquery, newvalues) </span><br></pre></td></tr></table></figure></p>
<h1 id="六、删除数据"><a href="#六、删除数据" class="headerlink" title="六、删除数据"></a>六、删除数据</h1><h4 id="删除一条数据"><a href="#删除一条数据" class="headerlink" title="删除一条数据"></a>删除一条数据</h4><p> 例子：删除集合中”name”为“lisi”的第一条数据<br>  <figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">myquery = &#123; &quot;name&quot;: &quot;lisi&quot; &#125;</span><br><span class="line">my_collection.delete_one(myquery) </span><br></pre></td></tr></table></figure></p>
<h4 id="删除多条数据"><a href="#删除多条数据" class="headerlink" title="删除多条数据"></a>删除多条数据</h4><p>例子：删除集合中”age”为10的所有数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">myquery = &#123; &quot;age&quot;: 10 &#125;</span><br><span class="line">my_collection.delete_many(myquery) </span><br></pre></td></tr></table></figure>
<h4 id="删除所有数据"><a href="#删除所有数据" class="headerlink" title="删除所有数据"></a>删除所有数据</h4><p> 例子：删除集合中所有数据<br> <figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">my_collection.delete_many(&#123;&#125;) </span><br></pre></td></tr></table></figure><br> 或者</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">my_collection.remove() </span><br></pre></td></tr></table></figure>
<h4 id="删除集合"><a href="#删除集合" class="headerlink" title="删除集合"></a>删除集合</h4><p> mycol.drop()</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">mycol.drop()</span><br></pre></td></tr></table></figure>

<h1 id="七、数据排序"><a href="#七、数据排序" class="headerlink" title="七、数据排序"></a>七、数据排序</h1><p>sort() 方法可以指定升序或降序排序。</p>
<p>sort() 方法第一个参数为要排序的字段，第二个字段指定排序规则，1 为升序，-1 为降序，默认为升序。</p>
<p>例子：将my_collection集合中的数据安装年龄升序排序</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find().sort(&quot;age&quot;,-1)</span><br></pre></td></tr></table></figure>

<h1 id="八、其他"><a href="#八、其他" class="headerlink" title="八、其他"></a>八、其他</h1><h4 id="跳过指定数量的数据"><a href="#跳过指定数量的数据" class="headerlink" title="跳过指定数量的数据"></a>跳过指定数量的数据</h4><p>例子：跳过10条数据后，取10条数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">result = my_collection.find().skip(10).limit(10)</span><br></pre></td></tr></table></figure>
<p>注：常用作分页</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/mongodb/" rel="tag"><i class="fa fa-tag"></i> mongodb</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/archives/60074.html" rel="prev" title="如何将本地项目添加到Git远程仓库？">
      <i class="fa fa-chevron-left"></i> 如何将本地项目添加到Git远程仓库？
    </a></div>
      <div class="post-nav-item">
    <a href="/archives/aa588849.html" rel="next" title="Android 序列化总结">
      Android 序列化总结 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="nav-number">1.</span> <span class="nav-text">一、准备工作</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E8%BF%9E%E6%8E%A5Mongodb"><span class="nav-number">2.</span> <span class="nav-text">二、连接Mongodb</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1%E3%80%81%E8%BF%9E%E6%8E%A5MongoDB"><span class="nav-number">2.0.0.1.</span> <span class="nav-text">1、连接MongoDB</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2%E3%80%81%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%95%B0%E6%8D%AE%E5%BA%93%EF%BC%8C%E6%88%96%E8%80%85%E4%BD%BF%E7%94%A8%E6%9F%90%E4%B8%AA%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">2.0.0.2.</span> <span class="nav-text">2、新建一个数据库，或者使用某个数据库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3%E3%80%81%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA%E9%9B%86%E5%90%88%EF%BC%8C%E6%88%96%E8%80%85%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E7%9A%84%E6%9F%90%E4%B8%AA%E9%9B%86%E5%90%88"><span class="nav-number">2.0.0.3.</span> <span class="nav-text">3、新建一个集合，或者使用数据库中的某个集合</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE"><span class="nav-number">3.</span> <span class="nav-text">三、添加数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%8F%92%E5%85%A5%E4%B8%80%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-number">3.0.0.1.</span> <span class="nav-text">插入一条数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%8F%92%E5%85%A5%E5%A4%9A%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-number">3.0.0.2.</span> <span class="nav-text">插入多条数据</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE"><span class="nav-number">4.</span> <span class="nav-text">四、查询数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E6%89%80%E6%9C%89%E6%95%B0%E6%8D%AE"><span class="nav-number">4.0.0.1.</span> <span class="nav-text">查询所有数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E4%B8%80%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-number">4.0.0.2.</span> <span class="nav-text">查询一条数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E5%A4%9A%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-number">4.0.0.3.</span> <span class="nav-text">查询多条数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9D%A1%E4%BB%B6%E6%9F%A5%E8%AF%A2"><span class="nav-number">4.0.0.4.</span> <span class="nav-text">条件查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%96"><span class="nav-number">4.0.0.5.</span> <span class="nav-text">或</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%8E"><span class="nav-number">4.0.0.6.</span> <span class="nav-text">与</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%9F%A5%E8%AF%A2"><span class="nav-number">4.0.0.7.</span> <span class="nav-text">使用正则表达式查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%94%E5%9B%9E%E6%8C%87%E5%AE%9A%E6%9D%A1%E6%95%B0%E8%AE%B0%E5%BD%95"><span class="nav-number">4.0.0.8.</span> <span class="nav-text">返回指定条数记录</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E4%BF%AE%E6%94%B9%E6%95%B0%E6%8D%AE"><span class="nav-number">5.</span> <span class="nav-text">五、修改数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BF%AE%E6%94%B9%E4%B8%80%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-number">5.0.0.1.</span> <span class="nav-text">修改一条数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BF%AE%E6%94%B9%E5%A4%9A%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-number">5.0.0.2.</span> <span class="nav-text">修改多条数据</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE"><span class="nav-number">6.</span> <span class="nav-text">六、删除数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E4%B8%80%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-number">6.0.0.1.</span> <span class="nav-text">删除一条数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E5%A4%9A%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-number">6.0.0.2.</span> <span class="nav-text">删除多条数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E6%89%80%E6%9C%89%E6%95%B0%E6%8D%AE"><span class="nav-number">6.0.0.3.</span> <span class="nav-text">删除所有数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E9%9B%86%E5%90%88"><span class="nav-number">6.0.0.4.</span> <span class="nav-text">删除集合</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81%E6%95%B0%E6%8D%AE%E6%8E%92%E5%BA%8F"><span class="nav-number">7.</span> <span class="nav-text">七、数据排序</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AB%E3%80%81%E5%85%B6%E4%BB%96"><span class="nav-number">8.</span> <span class="nav-text">八、其他</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%B7%B3%E8%BF%87%E6%8C%87%E5%AE%9A%E6%95%B0%E9%87%8F%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="nav-number">8.0.0.1.</span> <span class="nav-text">跳过指定数量的数据</span></a></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="John Doe"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">John Doe</p>
  <div class="site-description" itemprop="description">不积跬步，无以至千里；不积小流，无以成江海。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">47</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">John Doe</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
